package com.chap1;

/**
 * <p>
 * <code>EndThread</code>
 * </p>
 *
 * @author junbao3
 * TODO
 * @date 2023-03-20 14:26
 * Copyright (C) 2021 IFlyTek. All rights reserved.
 */
public class EndThread {
    public static void main(String[] args) throws InterruptedException {
//        UseRunnable useRunnable = new UseRunnable();
//        Thread thread1 = new Thread(useRunnable);
//        thread1.start();
//        Thread.sleep(20);
//        thread1.interrupt();


        UseThread useThread = new UseThread();
        useThread.start();
        Thread.sleep(20);
        useThread.interrupt();
    }

    private static class UseRunnable implements Runnable{

        @Override
        public void run() {
            String threadName = Thread.currentThread().getName();
            System.out.println(threadName+" interrupt flag = "+Thread.currentThread().isInterrupted());
            while(true){
                System.out.println(threadName+" is running");
                System.out.println(threadName+" inner interrupt flag = "+Thread.currentThread().isInterrupted());
            }
        }
    }

    private static class UseThread extends Thread{
        @Override
        public void run() {
            String threadName = Thread.currentThread().getName();
            System.out.println(threadName+" interrupt flag = "+isInterrupted());
            while(!isInterrupted()){
                System.out.println(threadName+" is running");
                /*
                老师上课的时候只是随便演示了一次，然后就说不会出现true
                实际，多演示几次，确实会出现true.
                一种比较极端的情况是，子线程当前运行到while循环条件判断那个位置，判断没有中断标志，进入while循环。
                主线程开始执行endThread.interrupt();这一句，这样子线程继续执行，就打印出了true
                */
                System.out.println(threadName+" inner interrupt flag = "+isInterrupted());
            }
        }
    }
}
